app.controller('one', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //下一步
    //安装详情
    $scope.az2 = function (caseId) {
        localStorage.removeItem("sdm-feSignUrl");
        localStorage.removeItem("sdm-customerSignUrl");

        az2.params.caseId = caseId;
        localStorage.setItem("sdm-caseId",caseId);
        $state.go(az2.stateName);
    }
    //DOA
    $scope.six = function (caseId) {
        six.params.caseId = caseId;
        localStorage.setItem("sdm-caseId",caseId);
        $state.go(six.stateName);
    }
    //DOA
    $scope.five = function () {
        $state.go(five.stateName);
    }
    //预约
    $scope.seven = function (caseId,dueDate) {
        seven.params.caseId = caseId;
        seven.params.dueDate = dueDate;
        localStorage.setItem("sdm-caseId",caseId);
        $state.go(seven.stateName);
    }
    //暂停
    $scope.eight = function (caseId,dueDate) {
        eight.params.caseId = caseId;
        eight.params.dueDate = dueDate;
        localStorage.setItem("sdm-caseId",caseId);
        $state.go(eight.stateName);
    }

    $scope.sixteen = function () {
        $state.go(sixteen.stateName);
    }
    $scope.seventeen = function () {
        $state.go(seventeen.stateName);
    }
    //录入基本信息
    $scope.az13 = function () {
        $state.go(az13.stateName);
    }
    //附件
    $scope.pv9 = function () {
        $state.go(pv9.stateName);
    }
    //工作人员签字
    $scope.pv11 = function () {
        $state.go(pv11.stateName);
    }
    //客户是否签字
    $scope.pv12 = function () {
        $state.go(pv12.stateName);
    }
    //客户签字
    $scope.pv13 = function () {
        $state.go(pv13.stateName);
    }
    //pv开始
    $scope.pv1_0 = function () {
        $state.go(pv1_0.stateName);
    }
    //pv任务
    $scope.pv3_0 = function () {
        $state.go(pv3_0.stateName);
    }
    //pv任务
    $scope.pv7 = function () {
        $state.go(pv7.stateName);
    }
    //pv预约
    $scope.pv5 = function () {
        $state.go(pv5.stateName);
    }
    $scope.pv6 = function () {
        $state.go(pv6.stateName);
    }
    //退回上一步
    $scope.one = function () {
        $state.go(one.stateName);
    }
    $scope.az13 = function () {
        $state.go(az13.stateName);
    }

    //跳转到我的页面
    $scope.jumpTo = function () {
        mine.params.type = "sdm";
        $state.go(mine.stateName);
    }


    //跳转到PV
    $scope.jumpToPV = function () {
        $state.go(pv1_0.stateName);
    }
    //会签
    $scope.jumpToSign = function () {
        //设置选中标记
        var signCaseIdAry = [];
        for(var i=0;i<$scope.pageData.length;i++){
            var obj = $scope.pageData[i];
            if(obj.flag){
                signCaseIdAry.push(obj.caseId);
            }
        }

        if(signCaseIdAry.length==0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请至少选择一个进行会签"));
            return;
        }
        localStorage.setItem("sdmSignCaseIdAry",JSON.stringify(signCaseIdAry));
        $state.go(confirmsign.stateName);
    }
    $scope.setFlag = function (item) {
        item.flag = !item.flag;
    }

    $scope.checkCustomeName = function () {
        $scope.customeFlag = true;
        $scope.$applyAsync();
    }
    //获得数据
    $scope.getOrders = function (status,contactFlag) {
        //localStorage.clear();
    	hpclearCache();

        $(".weui-picker").css("display","none");
        if("所有"==status){
            $scope.status = "";
        }else if(status){
            $scope.status = status;
        }
        var customerName = document.getElementById("input-customeName");
        if(customerName){
            $scope.customeName = customerName.value;
        }
//        else{
//        	$scope.customeName = sessionStorage.getItem("username");
//        }
        // $scope.status = status; //为下拉刷新做准备
		var postData = {
			currPage : $scope.currentPage,
            pageSize : $scope.pageSize,
			status : $scope.status,
            customeName : $scope.customeName
		};
        postHttp($http, 'installtionCase/list', postData, function (res) {
            $scope.customeFlag = false;
            if (res.code === 0 && res.msg == "success") {
                // $scope.currPage = res.data.currPage;
                $scope.totalCount = res.data.totalCount;
                // $scope.totalPage = res.data.totalPage;

                if ($scope.refreshFlag) {
                    $scope.pageData = [];
                    $scope.refreshFlag = false;
                }
                $scope.pageCount = res.data.totalPage;
                if(true == contactFlag){
                    $scope.pageData = $scope.pageData.concat(res.data.list || []);
                }else{
                    $scope.pageData = res.data.list;
                }

                //设置选中标记
                var ypd = 0;var yzt = 0; var dqz=0;
                for(var i=0;i<$scope.pageData.length;i++){
                    var obj = $scope.pageData[i];
                    obj.flag = false;

                    //统计已派单，已暂停，待签字个数
                    if(obj.status == 2){
                        ypd+=1;
                    }else if(obj.status == 4){
                        yzt+=1;
                    }else if(obj.status == 5){
                        dqz+=1;
                    }
                }
                $scope.summaryMessage = "已派单:"+ypd+"    已暂停:"+yzt+"     待签字:"+dqz;


                $scope.$applyAsync();
            } else {
                //$rootScope.tipPopups.show(($rootScope.tipMessage = res.message));
            	location.href="index.html";
            }
        });
    }

    function clearPathParam() {
        five.params = {};
        six.params = {};
        seven.params = {};
        eight.params = {};
        sixteen.params = {};
        az2.params = {};
        az13.params = {};
        countersign.params = {};
        confirmsign.params = {};

        //localStorage.clear();
        hpclearCache();
    }


    var options = {
        container: '#minirefresh',
        down: {
            isLock:true,
            callback: function () {
                // 下拉事件
                $scope.getOrders("");
                miniRefresh.endDownLoading();
            }
        },
        up: {
            isLock: false,
            isAuto: false,
            loadFull: {
                isEnable: false
            },
            offset: 200,
            isShowUpLoading: false,
            callback: function () {
                // 上拉事件
                $scope.nextPage();
                // 注意，由于默认情况是开启满屏自动加载的，所以请求失败时，请务必endUpLoading(true)，防止无限请求
                miniRefresh.endUpLoading(true, 'success');
            }
        }
    }
    var miniRefresh;
    setTimeout(function () {
        miniRefresh = new MiniRefresh(options);
    }, 1000);
    $scope.$watch('allData', function (newV, oldV) {
        if (newV != oldV)
            if (miniRefresh)
                miniRefresh.resetUpLoading();
    }, true)


    $scope.nextPage = function () {
        if ($scope.pageCount > $scope.currentPage) {
            $scope.currentPage++;
            $scope.getOrders("",true);
        }
    }

    //拨打电话
    $scope.phoneCall = function (phonenum,caseId) {
        var postData = {
            caseId:caseId
        };
        postHttp($http, 'installtionCase/saveContactTime', postData, function (res) {
            if (res.code === 0 && res.msg == "success") {
                window.location.href = "tel://"+phonenum;
            }
        });
    }

    //初始化
    function init(){
		 if(checkPhone("iphone")){
			try{
				window.webkit.messageHandlers.requestGPS.postMessage("");
			}catch(e){
				 $rootScope.tipPopups.show(($rootScope.tipMessage = JSON.stringify(e)));
			}
		 }

        clearPathParam();
        
        $scope.refreshFlag = true;
        $scope.currentPage = 1;
        $scope.pageSize = 10;
        $scope.pageCount = 1;
        $scope.customeName = "";
        $scope.customeFlag = false;
        localStorage.removeItem("sdmSignCaseIdAry");
        localStorage.removeItem("sdm-countfeSignUrl");
        localStorage.removeItem("sdm-countcustomerSignUrl");
        localStorage.removeItem("sdm-caseId");

        $scope.getOrders("");
        $scope.$applyAsync();
    }

    init();

    reloadJs();
});

function getSdmOrders(param) {
    var $scope = getScope("one");
    $scope.getOrders(param,false);
}
//检查输入框
function checkCustomeName() {
    var $scope = getScope("one");
    $scope.checkCustomeName();
}